<?php


namespace Page;


class Find
{
    /**
     * @param $model 模型对象
     * @param $param 传过来的参数
     * @param $where 所有的查询条件
     */
    public function find($model, $param, $where)
    {
        $all_param = $this->get_all_param($param);
        $new_where = $this->get_where($param, $all_param, $where);
        $result = $this->get_data($model, $param, $new_where);
        return $result;
    }
    # 组装查询条件
    private function get_where($param, $all_param, $where)
    {
        # 把不存在的参数设置为空
        foreach ($all_param as $value){
            if(array_key_exists($value, $param['field']) == false){
                $filed[$value] = '';
            }
        }

        $where_new = [];
        # 组装新的where条件
        foreach ($param['field'] as $key=>$value){
            if($value == ''){
                unset($where[$key]);
                continue;
            }
            array_push($where_new, $where[$key]);
        }
        return $where_new;
    }
    # 调用layui分页接口
    private function get_data($model, $param, $where)
    {
        $page = new Page($model, $param['page'], $param['limit'], [], null, $where);
        $result = $page->layuiPage();
        return $result;
    }
    # 获取所有的参数组成一个新的数组
    private function get_all_param($param)
    {
        $all_param = [];
        foreach ($param['field'] as $key=>$value){
            array_push($all_param, $key);
        }
        return $all_param;
    }
}